<%@ page language="java"  pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    	<style type="text/css">
		* {
			font-size: 12px;
		}
    	</style>
		<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery/themes/icon.css">
		<script type="text/javascript" src="<%=basePath%>js/jquery/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>js/jquery/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>js/jquery/locale/easyui-lang-zh_CN.js"></script>
 		<script type="text/javascript" src="<%=basePath%>js/ztree/js/jquery.ztree.core-3.5.js"></script>
 		<script type="text/javascript" src="<%=basePath%>js/ztree/js/jquery.ztree.excheck-3.5.js"></script>
 	    <link rel="stylesheet" href="<%=basePath%>js/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
 		<script type="text/javascript">
 		var ztreeObj ;
		var vailflag=false;
		var editflag = false;
 			function sub()
		{
			$('#ff').submit();
			vailflag = false ;
		}
		
		function subResource()
		{
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getCheckedNodes(true);
			var ids = [] ;
			for(var i = 0 ; i < nodes.length ; i ++)
			{
					ids.push(nodes[i].id);
			}
			var rows = $('#dataGrid').datagrid('getSelections');
			$.ajax({
				type:"post",
				url:"<%=basePath%>sys/role/roleAction!saveResourceRole.action",
				data:{roleid:rows[0].roleid,resourcesid:ids.join(",")},
				success:function (data){
					var json = eval("("+data+")");
						if(!json.state)
						{
							alert(json.msg);
						}else {
							alert("保存成功");
							closeRW();
						}
				}
			});
		}
		//手机资源授权提交
		function subMobileResource()
		{
			var treeObj = $.fn.zTree.getZTreeObj("MobiletreeDemo");
			var nodes = treeObj.getCheckedNodes(true);
			var ids = [] ;
			for(var i = 0 ; i < nodes.length ; i ++)
			{
					ids.push(nodes[i].id);
			}
			//alert("ids:"+ids);
			var rows = $('#dataGrid').datagrid('getSelections');
			$.ajax({
				type:"post",
				url:"<%=basePath%>sys/role/roleAction!saveMobileResourceRole.action",
				data:{roleid:rows[0].roleid,resourcesid:ids.join(",")},
				success:function (data){
					var json = eval("("+data+")");
						if(!json.state)
						{
							alert(json.msg);
						}else {
							alert("保存成功");
							closeMobileRW();
						}
				}
			});
		}
		
		function closeW()
		{
			$('#logicWindow').window('close');
		}
		
		function showEdit()
		{
		editflag=true;
			var rows = $('#dataGrid').datagrid('getSelections');
			if(rows.length != 1)
			{
				$.messager.alert('提示','请选中一条记录');
			}else {
				$('#ff').form({
				onSubmit:function (){
					var t =  $(this).form('validate');
					if(t)
					{
						$('#btnsub').linkbutton({disabled:true});
					}
					return t;
				},
				success:function(data){
					var json = eval("("+data+")");
					if(!json.state)
					{
						alert(json.msg);
					}
					$('#btnsub').linkbutton({disabled:false});
					closeW();
					$("#dataGrid").datagrid("reload");
				}
			});
				var now=new Date();
				var number = now.getYear().toString()+now.getMonth().toString()+now.getDate().toString()+now.getHours().toString()+now.getMinutes().toString()+now.getSeconds().toString();
				$('#ff').form('load','<%=basePath%>sys/role/roleAction!loadString.action?roleEntity.roleid='+rows[0].roleid+'&t='+number);
				$('#logicWindow').window('open');
			}
			document.getElementById('ff').action = '<%=basePath%>sys/role/roleAction!updateString.action' ;
		}
		
		function deleteLogic()
		{
			var rows = $('#dataGrid').datagrid('getSelections');
			if(rows.length >0 )
			{
				var ids = []; 
				for (var i = 0 ; i < rows.length ; i ++)
				{
					if(rows[i].system == 0)
					{
						alert('系统角色不允许删除');
						return ;
					}
					ids.push(rows[i].roleid);
				}
				var now=new Date();
				var number = now.getYear().toString()+now.getMonth().toString()+now.getDate().toString()+now.getHours().toString()+now.getMinutes().toString()+now.getSeconds().toString();
		
				$.ajax({
					url:"<%=basePath%>sys/role/roleAction!deleteString.action?deleteid="+ids.join(",")+"&rand="+Math.random(),
					success:function (data){
					var json = eval("("+data+")");
						if(!json.state)
						{
							alert(json.msg);
						}else {
							$("#dataGrid").datagrid("reload");
						}
					}
				});
			}else {
				$.messager.alert('提示','请选择需要删除的数据');
			}
		
		}
			
 			$(document).ready(function (){
 			setTimeout(this,1000);
			$('#dataGrid').datagrid({
				url:"<%=basePath%>role/getRoleList.xhtml",
				width:"auto",
				pageSize:20,
				title:"角色管理",
				iconCls:'icon-save',
				pagination:true,
				striped:true,
				loadMsg:"加载中...",
				rownumbers:true,
				collapsible:true,
				fit:true,
				frozenColumns:[[ 
                {field:'id',checkbox:true}
				]], 
				toolbar:[{
					id:"btnadd",
					text:"新增角色",
					iconCls: "icon-add",
					handler:function ()
					{
						addW();
					}
				},{
					id:"btnedit",
					text:"修改角色信息",
					iconCls: "icon-edit",
					handler:function()
					{
						showEdit();
					}
				},{
					id:"btndelete",
					text:"删除角色",
					iconCls: "icon-remove",
					handler:function()
					{
						deleteLogic();
					}
				},{
					id:"btnResource",
					text:"角色资源",
					iconCls : "icon-ok",
					handler:function (){
						roleResource();
					} 
					
				},{
					id:"btnMobileResource",
					text:"手机用户菜单",
					iconCls : "icon-ok",
					handler:function (){
						btnMobileResource();
					} 
					
				}],
				columns:[[
					{field:"rolename",title:"角色名称",width:"150"},
					{field:"txt",title:"备注",width:"150"},
					{field:"createtime",title:"创建时间",width:"150"},
					{field:"system",title:"创建时间",width:"150",hidden:"true"}
				]]
			});	
			});	
			
			function roleResource()
			{
				var rows = $('#dataGrid').datagrid('getSelections');
				if(rows.length  == 1 )
				{
					
					setCheck(rows[0].roleid);
					$('#roleResourceWindow').window('open');
				}else {
					alert('请选择一条记录');
				}
			}
			
			function btnMobileResource()
			{
			//alert("ok");
				var rows = $('#dataGrid').datagrid('getSelections');
				if(rows.length  == 1 )
				{
					setMobileCheck(rows[0].roleid);
					$('#btnMobileResourceWindow').window('open');
				}else {
					alert('请选择一条记录');
				}
			}
		var setting = {
					async: {
						enable: true,
						
						autoParam: ["id"]
					},
					check: {
						enable: true
					},
					data: {
						simpleData: {
							idKey: "id",
							pIdKey:"pid",
							enable: true
						}
					}
				};
		function setCheck(v) {
			setting.async.url = "<%=basePath %>sys/role/roleAction!getResourceRole.action?roleid="+v;
			 ztreeObj = $.fn.zTree.init($("#treeDemo"), setting, null);
			ztreeObj.setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
			
		}
		
		function setMobileCheck(v) {
			setting.async.url = "<%=basePath %>sys/role/roleAction!getMobileResourceRole.action?roleid="+v;
			 ztreeObj = $.fn.zTree.init($("#MobiletreeDemo"), setting, null);
			ztreeObj.setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
			
		}
		
			function addW()
			{
			editflag = true;
					$('#roleSpan').html('');
					$('#ff').form('clear');
					$('#ff').form({
						onSubmit:function (){
							var t =  $(this).form('validate');
							if(t)
							{
								$('#btnsub').linkbutton({disabled:true});
							}
							return t;
						},
						success:function(data){
							var json = eval("("+data+")");
							if(!json.state)
							{
								alert(json.msg);
							}
							$('#btnsub').linkbutton({disabled:false});
							closeW();
							$("#dataGrid").datagrid("reload");
						}
					});
					document.getElementById('ff').action = '<%=basePath%>role/addRole.xhtml' ;
					$('#logicWindow').window('open');
			}
			
	
		function closeRW()
		{
			$('#roleResourceWindow').window('close');
		}
		function closeMobileRW()
		{
			$('#btnMobileResourceWindow').window('close');
		}
		function validAjaxRolename(obj){
			if(!obj.value || obj.value==""){
					
				}else if(editflag){
					vailflag = true;
				}else{
								$.ajax({
				type:"post",
				data:{"rolename":obj.value},
				url:"<%=basePath %>sys/role/roleAction!validRolename.action",
				success:function(data){
					if(data>0){
						$('#roleSpan').html('<font color="red">已存在的角色名</font>');
						document.getElementById("rolename").focus();
						vailflag = false ;
					}else if(data == 0){
						$('#roleSpan').html('');
						vailflag = true ;
					}
				}
			});	
				}
		}
		
 		</script>
  </head>
  
  <body>
    	<table id = "dataGrid" ></table>
    	
    	
		<div id="logicWindow" class="easyui-window" title="角色操作" iconCls="icon-save" style="width: 500px; height: 200px; padding: 5px;" closed = "true"  >
			<div class="easyui-layout" fit="true">
				<div region="center" border="false"
					style="background: #fff; border: 1px solid #ccc;">
					<form id="ff" method="post">

						<input type="hidden" name="roleid" id="roleid">
						<table>
							<tr>
								<td>
									<label for="classname">
										角色名称:
									</label>
								</td>
								<td>
									<input class="easyui-validatebox" onblur="validAjaxRolename(this)" validType="length[0,20]" type="text" style="width: 200" id="rolename" name="rolename" required="true"></input>
									<span id = "roleSpan" ></span>
								</td>
							</tr>
							
							<tr>
								<td>
									<label for="txt">
										备注说明:
									</label>
								</td>
								<td>
									<textarea name="txt" validType="length[0,200]" id="txt" style="height: 80px; width: 300px" required="true"></textarea>
								</td>
							</tr>
						</table>
					</form>
				</div>
				<div region="south" border="false"
					style="text-align: right; padding: 5px 0;">
					<a class="easyui-linkbutton" id="btnsub" iconCls="icon-ok"
						href="#" onclick = "sub()">提 交</a>
					<a class="easyui-linkbutton" iconCls="icon-cancel"
						href="#" onclick = "closeW()">关闭</a>
				</div>
			</div>
		</div>
		
		
		<div id = "roleResourceWindow" class="easyui-window" closed = "true" modal = "true" style = "height:400px;width:200px;" title = "角色资源" iconCls="icon-save" >
			<div class="easyui-layout" fit="true">
				
				<div region = "center" >
					<ul id="treeDemo"  class="ztree"></ul>
				</div>
				<div region="south" border="false"
					style="text-align: right; padding: 5px 0;">
					<a class="easyui-linkbutton" id="btnsub" iconCls="icon-ok"
						onclick = "subResource()">提 交</a>
					<a class="easyui-linkbutton" iconCls="icon-cancel"
						onclick = "closeRW()">关闭</a>
				</div>
			</div>
			
		</div>
		
		<div id = "btnMobileResourceWindow" class="easyui-window" closed = "true" modal = "true" style = "height:400px;width:200px;" title = "手机用户菜单" iconCls="icon-save" >
			<div class="easyui-layout" fit="true">
				
				<div region = "center" >
					<ul id="MobiletreeDemo"  class="ztree"></ul>
				</div>
				<div region="south" border="false"
					style="text-align: right; padding: 5px 0;">
					<a class="easyui-linkbutton" id="btnsub" iconCls="icon-ok"
						onclick = "subMobileResource()">提 交</a>
					<a class="easyui-linkbutton" iconCls="icon-cancel"
						onclick = "closeMobileRW()">关闭</a>
				</div>
			</div>
			
		</div>
  </body>
</html>
